package com.resou.reader.reader.end;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.resou.reader.R;
import com.resou.reader.api.entry.BookDetailBean;
import com.resou.reader.base.ResouBaseActivity;
import com.resou.reader.bookdetail.BookDetailActivity;
import com.resou.reader.bookdetail.comment.CommentListActivity;
import com.resou.reader.mine.IView.UserInstance;
import com.resou.reader.reader.adpter.EndBookAdapter;
import com.resou.reader.utils.ToastUtil;
import com.resou.reader.utils.WXUtils;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 阅读器 读到最后一页的推荐页
 */
public class EndBookActivity extends ResouBaseActivity<EndBookPresenter> implements ContractEndBookInterface.IEndBookView, UMShareListener {
    public static final String BOOKID = "BOOKID";
    public static final String SHAREURL = "SHAREURL";
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.mTitleBar)
    RelativeLayout mTitleBar;
    @BindView(R.id.ivBack)
    ImageView ivBack;
    @BindView(R.id.mScrollView)
    ScrollView mScrollView;
    @BindView(R.id.ivBookStateText)
    ImageView ivBookStateText;
    @BindView(R.id.ivHeaderPic)
    ImageView ivHeaderPic;
    @BindView(R.id.tvShare)
    TextView tvShare;
    @BindView(R.id.tvCommentNum)
    TextView tvCommentNum;
    @BindView(R.id.tvUpdataBookList)
    TextView tvUpdataBookList;
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    EndBookAdapter endBookAdapter;

    String bookId = "";
    String shareUrl = "";
    BookDetailBean bookDetailBean;

    public static void start(Context context, String bookId, String shareUrl) {
        Intent starter = new Intent(context, EndBookActivity.class);
        starter.putExtra(BOOKID, bookId);
        starter.putExtra(SHAREURL, shareUrl);
        context.startActivity(starter);
        if (context instanceof Activity) {
            ((Activity) context).overridePendingTransition(R.anim.anim_detail_enter, R.anim.anim_no);
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_end_book;
    }

    @Override
    public void retryRefreshData() {
        mPresenter.quearBookDetail(bookId);
        mPresenter.quearRecommentBookList(bookId);
    }

    @Override
    protected void initView() {

        tvTitle.setText("");
        bookId = getIntent().getStringExtra(BOOKID);
        shareUrl = getIntent().getStringExtra(SHAREURL);
        mScrollView.smoothScrollTo(0, 0);
        endBookAdapter = new EndBookAdapter(R.layout.endbookadapter, null);
        endBookAdapter.openLoadAnimation();
        mRecyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        mRecyclerView.setAdapter(endBookAdapter);
        mRecyclerView.setNestedScrollingEnabled(false);

    }

    @Override
    public void initData() {
        if (mPresenter == null) {
            initPresenter();
        }
        mPresenter.quearBookDetail(bookId);
        mPresenter.quearRecommentBookList(bookId);
        endBookAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                BookDetailBean bookDetailBean = (BookDetailBean) adapter.getData().get(position);
                BookDetailActivity.startDetailActivity(EndBookActivity.this, bookDetailBean.getId());
            }
        });
        /**
         * 防止首次进入该页面 没有到顶部
         */
        mScrollView.smoothScrollTo(0, 20);
        ivHeaderPic.setFocusable(true);
        ivHeaderPic.setFocusableInTouchMode(true);
        ivHeaderPic.requestFocus();
    }

    @Override
    public void showContent() {
        dissLoading();
    }

    @Override
    public void showProgress() {
        showLoading();
    }

    @Override
    public void showError(Throwable throwable) {
        dissLoading();
        ToastUtil.makeShortToast("网络异常");
        endBookAdapter.getData().clear();
        endBookAdapter.notifyDataSetChanged();
        showNetErrorView(R.id.rlContent);
    }

    @Override
    public void showEmpty() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mPresenter != null) {
            mPresenter.quearCommentNum(bookId);
        }
    }

    @Override
    protected void initStatusBar() {

    }

    @Override
    protected void initPresenter() {
        if (mPresenter == null) {
            mPresenter = new EndBookPresenter(this);
        }
    }


    @OnClick({R.id.ivBack, R.id.tvShare, R.id.tvCommentNum, R.id.tvUpdataBookList})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ivBack:
                finish();
                break;
            case R.id.tvShare:
                if (isNetwork()) {
                    mPresenter.share(this, shareUrl, bookDetailBean, this);
                }
                break;
            case R.id.tvCommentNum:
                CommentListActivity.start(this, bookId, "");
                break;
            case R.id.tvUpdataBookList:
                mPresenter.quearRecommentBookList(bookId);
                break;
        }
    }

    @Override
    public void recommentBook(List<BookDetailBean> list) {
        endBookAdapter.setNewData(list);
    }

    @Override
    public void showCommentNum(String num) {
        tvCommentNum.setText("评论" + num);
    }

    @Override
    public void showBookDetail(BookDetailBean bookDetailBean) {
        this.bookDetailBean = bookDetailBean;
        tvTitle.setText(bookDetailBean.getNovelName() + "");
        if (bookDetailBean.getSerialStatus() == 3) {
            ivBookStateText.setImageResource(R.mipmap.ic_endbook_text);
            ivHeaderPic.setImageResource(R.mipmap.ic_endbook_male);
        } else {
            ivBookStateText.setImageResource(R.mipmap.ic_endbook_text2);
            ivHeaderPic.setImageResource(R.mipmap.ic_endbook_girl);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onStart(SHARE_MEDIA share_media) {
        switch (share_media) {
            case WEIXIN:
            case WEIXIN_CIRCLE:
                if (!WXUtils.isWeixinAvilible(this)) {
                    ToastUtil.makeShortToast("您未安装微信，请选择其他分享方式");
                    return;
                }
                break;
            case QZONE:
            case QQ:
                if (!WXUtils.isQQClientAvailable(this)) {
                    ToastUtil.makeShortToast("您未安装QQ，请选择其他分享方式");
                    return;
                }
                break;
        }
    }

    @Override
    public void onResult(SHARE_MEDIA share_media) {
        ToastUtil.makeShortToast("分享成功");
    }

    @Override
    public void onError(SHARE_MEDIA share_media, Throwable throwable) {

    }

    @Override
    public void onCancel(SHARE_MEDIA share_media) {
        ToastUtil.makeShortToast("取消分享");
    }
}
